<template>
  <div class="home-page">
    <van-tabs animated v-model="activeChannelId">
      <van-tab
        v-for="item in $store.state.channel.myChannels"
        :key="item.id"
        :title="item.name"
        :name="item.id"
      >
        <article-list :channelId="item.id"></article-list>
      </van-tab>
    </van-tabs>
    <!-- 按钮 -->
    <div class="btn-wrapper">
      <i class="iconfont icon-search"></i>
      <i
        class="iconfont icon-channel"
        @click="$store.commit('channel/showPopup')"
      ></i>
    </div>
    <!-- 频道 -->
    <article-channel-popup></article-channel-popup>
  </div>
</template>
<script>
// getAllChannels,

import ArticleList from './components/article-list.vue'
import ArticleChannelPopup from './components/article-channel-popup.vue'
export default {
  computed: {
    activeChannelId: {
      get () {
        return this.$store.state.channel.activeChannelId
      },
      set (newActiveChannelId) {
        this.$store.commit('channel/setActiveChannelId', newActiveChannelId)
      }
    }
  },
  components: { ArticleList, ArticleChannelPopup },
  async created () {
    const [err] = await this.$store.dispatch('channel/loadMyChannels')
    if (err) return this.$toast.fail('频道获取失败')
  },

  name: 'HomePage',
  watch: {
    async '$store.state.token' () {
      // 当token变化后，重新加载我的频道列表
      await this.$store.dispatch('channel/loadMyChannels')
      // 重置当前频道为推荐
      this.$store.commit('channel/setActiveChannelId', 0)
    }
  }

  // async created () {
  //   // 通过数组解构语法提取err和res，如果err为null说明没有异常
  //   const [err, res] = await getAllChannels()
  //   if (err) return console.log('失败', err.message)
  //   console.log('成功', res.data)
  // }
}
</script>
<style scoped lang="less">
.home-page {
  .btn-wrapper {
    position: absolute;
    right: 0;
    top: 0;
    width: 86px;
    height: 44px;
    background: #fff;
    display: flex;
    align-items: center;
    .iconfont {
      flex: 1;
      text-align: center;
      font-size: 18px;
    }
    &::before {
      content: "";
      width: 20px;
      height: 44px;
      position: absolute;
      left: -20px;
      top: 0;
      background: linear-gradient(to right, rgba(255, 255, 255, 0), #fff);
    }
  }
  ::v-deep .van-tabs {
    height: 100%;
    display: flex;
    flex-direction: column;
    .van-tabs__line {
      background: @geek-color;
      height: 2px;
      width: 32px;
    }
    .van-tab {
      color: #9ea1ae;
    }
    .van-tab--active {
      font-size: 18px;
      color: #333;
    }
    .van-tabs__wrap {
      padding-right: 86px;
      box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
    }
    .van-tabs__content {
      flex: 1;
      overflow: hidden;
    }
    .van-tab__pane {
      height: 100%;
    }
  }
}
</style>
